package com.gxa.uhome.service.impl;

import com.gxa.uhome.exception.BusinessException;
import com.gxa.uhome.mapper.UserMapper;
import com.gxa.uhome.pojo.User;
import com.gxa.uhome.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * User: Sanruy Mu
 * Date: 2021/06/07 19:09
 * Description:
 * Version: V1.0
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;

    @Override
    public Integer register(User user) throws BusinessException {
        Integer add = userMapper.add(user);
        if (add != 0) {
            return add;
        } else {
            throw new BusinessException("添加失败，请联系管理员");
        }
    }

    @Override
    public User selectByPhone(String phone) throws BusinessException {
        User user = userMapper.selectByPhone(phone);
        return user;
    }

    @Override
    public List<User> selectUser() throws BusinessException {
        List<User> users = userMapper.selectUser();
        if (users != null) {
            return users;
        } else {
            throw new BusinessException("查询，请联系管理员");
        }

    }
    @Override
    public List<Integer> selectStationName(String stationName) throws BusinessException {
        List<Integer> integer = userMapper.selectStationName(stationName);
        System.out.println(integer.size());
        if(integer.size()!=0 && !integer.isEmpty() && integer !=null ){
            System.out.println("+");
            return integer;
        }else{
            System.out.println(1);
            throw new BusinessException("该条件未查询到数据，请选择正确条件重新查询");
        }
    }

    @Override
    public List<User> selectByMany(Map<String, Object> map) throws BusinessException {
        List<User> users = userMapper.selectByMany(map);
        System.out.println(map);
        if(!users.isEmpty()){
            return users;
        }{
            throw new BusinessException("查询失败，请选择正确的查询条件进行查询");
        }
    }

    @Override
    public Integer updateLastAcTime(Integer id, Timestamp lastAcTime) throws BusinessException {
        Integer integer = userMapper.updateLastAcTime(id,lastAcTime);
        if(integer!=0){
            return  integer;
        }else {
            throw new BusinessException("操作失败");
        }

    }




    @Override
    public Integer changeStatus(Integer id,Boolean status) throws BusinessException {
        Integer integer = userMapper.updateStatus(id,status);
        if (integer != 0) {
            return integer;
        } else {
            throw new BusinessException("状态更改失败");
        }
    }

    @Override
    public User selectById(Integer id) throws BusinessException {
        User user = userMapper.selectById(id);
        if (user != null) {
            return user;
        } else {
            throw new BusinessException("查询失败");
        }
    }

    @Override
    public List<User> selectAll() throws BusinessException {
        List<User> users = userMapper.selectAll();
        if(!users.isEmpty()){
            return users;
        }else{
            throw new BusinessException("查询失败，请联系管理员进行核实");
        }

    }

    @Override
    public List<User> selectByPage(Integer page, Integer size) throws BusinessException {
        page = (page-1)*size;
        List<User> users = userMapper.selectByPage(page,size);
        if(!users.isEmpty()){
            return users;
        }else{
            throw new BusinessException("分页查询失败");
        }
    }

}